package com.hc.contorl;

import com.hc.pojo.TbUser;
import com.hc.service.TbUserService;
import com.hc.service.impl.TbUserServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet(value = "/userAdd")
public class UserAddContorller extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String pageSize=req.getParameter("pageSize");
        String username = req.getParameter("userName");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        TbUserService service = new TbUserServiceImpl();
        TbUser user = new TbUser();
        user.setUserName(username);
        user.setEmail(email);
        password = DigestUtils.md5Hex(password);
        user.setPassword(password);
        user.setPhone(phone);
        Date date=new Date();
        user.setCreateTime(date);
        user.setUpdateTime(date);
        boolean bool = service.addUser(user);
        if (bool) {
            req.setAttribute("message", "添加成功");

        } else {
            req.setAttribute("message", "添加失败");
        }
        req.setAttribute("reqPageSize",pageSize);
        req.setAttribute("reqPageNum",-1);
        req.getRequestDispatcher("/user_form.jsp").forward(req, resp);
    }
}
